<?php

require_once('Xiachufang/util/Mysql.php');
require_once('Xiachufang/util/EntityCache.php');

class MainConfig {
    static $config = array(
    
    'mem_cache' => array("127.0.0.1"),
    'enable_slaver'=>true,
    "webmaster" => "comdeng@gmail.com",
    "connections" => array(
		"main_db"=>array(
			"host"=>"127.0.0.1",
			"user"=>"root",
			"pass"=>"",
			"database"=>"xiachufang",
			"slavers" => array(
	            array(
	            'host'	=> '127.0.0.1',
	            'user'	=> 'root',
	            'pass'	=> '',
	            'enable' => false
	            )
	        )
		 ),
	)
	);

    static function init($configPath) {
        require($configPath);//由configPath提供配置信息
        self::$config = $mainConfig;
        
        EntityCache::init($mainConfig["mem_cache"]);
    }

    static function getConfig($name,$default=NULL) {
        if (isset(self::$config[$name])) {
            return self::$config[$name];
        }
        else {
            return $default;
        }
    }

    static $db = NULL;

    /**
     *
     * @return Mysql mysql
     */
    static function dbConnect() {
        if (self::$db!=NULL) {
            return self::$db;
        }
        $connections = MainConfig::getConfig("connections");
        $rssreader = $connections["rssreader_db"];
        $db = Mysql::connect($rssreader["host"],$rssreader["user"],$rssreader["pass"],$rssreader["database"]);
        if ($db===false) {
            throw new Exception("无法访问数据库");
        }
        else {
            self::$db = $db;
            return $db;
        }
    }

    /**
     * get master db connection.
     *
     * @return Mysql mysql db instance.
     */
    static function dbMasterConnect() {
        return self::dbConnect();
    }

    private static $forceUseMasterDb = false;
    private static $slaverDb = NULL;

    /**
     *
     * @return Mysql slave mysql instance
     */
    static function dbSlaverConnect() {
        if (self::$db!=NULL) {//如果已经建立到master的连接,不再连接slaver
            return self::$db;
        }
        if (self::$forceUseMasterDb) {
            return self::dbMasterConnect();
        }
        if (self::$slaverDb!=NULL) {
            return self::$slaverDb;
        }
        $connections = MainConfig::getConfig("connections");
        $slavers = $connections["rssreader_db"]["slavers"];
        $count = count($slavers);
        if ($count==0) {
            self::$slaverDb = MainConfig::dbConnect();
            return self::$slaverDb;
        }
        if (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        else {
            $ip = "127.0.0.1";
        }
        $ipLong = ip2long($ip);
         
        $index = $ipLong*2654435761%$count;
        $i = $index;
        do {
            $server = $slavers[$i];
            if ($server['enable']) {
                break;
            }
            $server = NULL;
            $i = ($i+1)%$count;
        }while($i!=$index);

        if ($server==NULL) {
            self::$slaverDb = MainConfig::dbConnect();
            return self::$slaverDb;
        }

        $db = Mysql::connect($server['host'],$server['user'],$server['pass'],$server['database']);
        if ($db===false) {
            throw new Exception("无法访问数据库");
        }
        else {
            self::$slaverDb = $db;
            return $db;
        }
    }
}

?>